package cz.hanis.android.geography.activities;

import java.util.ArrayList;
import java.util.List;

import cz.hanis.android.geography.R;
import cz.hanis.android.geography.TestActivity;
import cz.hanis.android.geography.WrongResourceException;
import cz.hanis.android.geography.models.Country;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class CountryFlagTestActivity extends TestActivity {

	private static final String TAG = "CountryFlagTestActivity";

	private ArrayList<String> answerIdList;
	private TextView tvCountry;
	private int correct;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTestLayout(R.layout.country_flag_test);
		setQuestion(getString(R.string.tv_country_flag_question));
		tvCountry = (TextView) findViewById(R.id.tv_country_flag_country);
		setQuestionView(tvCountry);
		addAnswerView((ImageView) findViewById(R.id.iv_country_flag_flag_1));
		addAnswerView((ImageView) findViewById(R.id.iv_country_flag_flag_2));
		addAnswerView((ImageView) findViewById(R.id.iv_country_flag_flag_3));
		addAnswerView((ImageView) findViewById(R.id.iv_country_flag_flag_4));
		answerIdList = new ArrayList<String>();

		setListeners();

		if (savedInstanceState == null) {
			newRound();
		} else {
			tvCountry.setText(savedInstanceState.getString(QUESTION_KEY));
			correct = savedInstanceState.getInt(CORRECT_POSITION_KEY);
			answerIdList = savedInstanceState
					.getStringArrayList(ANSWER_LIST_KEY);
			for (int i = 0; i < getNumberOfAnswers(); i++) {
				setFlagImage(i, answerIdList.get(i));
			}
			restoreRound();
		}

	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt(CORRECT_POSITION_KEY, correct);
		outState.putString(QUESTION_KEY, tvCountry.getText().toString());
		outState.putStringArrayList(ANSWER_LIST_KEY, answerIdList);
	}

	@Override
	protected int onAnswer(int answer) {
		int result = TestActivity.ANSWERE_NONE;
		setBackgroudButton(correct, R.drawable.button_9patch_green);
		if (answer == correct) {
			result = TestActivity.ANSWERE_CORRECT;
		} else if (answer == TestActivity.ANSWERE_TIMED_OUT) {
			result = TestActivity.ANSWERE_TIMED_OUT;
		} else {
			setBackgroudButton(answer, R.drawable.button_9patch_red);
			result = TestActivity.ANSWERE_INCORRECT;
		}
		return result;
	}

	@Override
	protected void onNewRound() {
		List<Country> randomCountries = getTools().getRandomCountries(
				getNumberOfAnswers(), getTest().getContinent());
		correct = getTools().getRandom(getNumberOfAnswers());
		String name = randomCountries.get(correct).getName();
		tvCountry.setText(name);

		answerIdList.clear();
		for (int i = 0; i < getNumberOfAnswers(); i++) {			
			String countryId = randomCountries.get(i).getId();
			answerIdList.add(countryId);
			setFlagImage(i, countryId);
		}
	}

	private void setFlagImage(int index, String countryId) {
		setBackgroudButton(index, R.drawable.button_9patch);
		ImageView flag = (ImageView) getAnswerViews().get(index);
		try {
			flag.setImageDrawable(getGeogrephyApplication().getFlagDrawable(
					countryId));
		} catch (WrongResourceException e) {
			Log.d(CountryFlagTestActivity.TAG, e.getMessage());
		}
	}

	@Override
	protected void onRestoreRound() {
		// TODO Auto-generated method stub

	}

}
